﻿using DG.Tweening;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;


public delegate void VoidDelegate();

public class ShuaiFunctions
{
    public static RaycastHit2D Raycast(Vector2 pos, Vector2 offset, Vector2 rayDirection, float length, LayerMask layer, Color color)
    {
        RaycastHit2D hit = Physics2D.Raycast(pos + offset, rayDirection, length, layer);
        Debug.DrawRay(pos + offset, rayDirection * length, color);

        return hit;
    }

    public static RaycastHit2D[] RaycastAll(Vector2 pos, Vector2 offset, Vector2 rayDirection, float length, LayerMask layer, Color color)
    {
        RaycastHit2D[] hits = Physics2D.RaycastAll(pos + offset, rayDirection, length, layer);
        Debug.DrawRay(pos + offset, rayDirection * length, color);

        return hits;
    }

    public static IEnumerator ShuaiWaitForSeconds(Action action, float time)
    {
        yield return new WaitForSeconds(time);
        action();
    }

    public static IEnumerator ShuaiWaitForSecondsRealtime(Action action, float time)
    {
        yield return new WaitForSecondsRealtime(time);
        action();
    }

    //设置所有子物体可见或者不可见
    public static void SetAllChildrenAtive(Transform parent, bool value)
    {
        for (int i = 0; i <= parent.childCount - 1; i++)
        {
            Transform child = parent.GetChild(i);
            child.gameObject.SetActive(value);
        }
    }

    //获取闲置的子物体，找不到就创建一个新的子物体
    public static Transform GetFreeChild(Transform parent)
    {
        Transform c = null;
        for (int i = 0; i <= parent.childCount - 1; i++)
        {
            Transform child = parent.GetChild(i);
            if (child.gameObject.activeSelf == false)
            {
                c = child;
                break;
            }
        }

        if (c == null)
        {
            GameObject go = GameObject.Instantiate(parent.GetChild(0).gameObject, Vector3.zero, Quaternion.identity, parent.transform);
            c = go.transform;
        }

        c.gameObject.SetActive(true);

        return c;
    }

    //DOTween延迟函数
    public static void DelayFunction(VoidDelegate callback, float time = 1f)
    {
        float timeCount = 0;
        DOTween.To(() => timeCount, a => timeCount = a, 0, time).OnComplete(() =>
            callback()
        );
    }

    // Number随机数个数
    // minNum随机数下限
    // maxNum随机数上限
    public static int[] GetRandomArray(int Number, int minNum, int maxNum)
    {
        int j;
        int[] b = new int[Number];
        for (j = 0; j < Number; j++)
        {
            int i = UnityEngine.Random.Range(minNum, maxNum + 1);
            int num = 0;
            for (int k = 0; k < j; k++)
            {
                if (b[k] == i)
                {
                    num = num + 1;
                }
            }
            if (num == 0)
            {
                b[j] = i;
            }
            else
            {
                j = j - 1;
            }
        }
        return b;
    }

}
